package com.shang.controller;

import com.shang.common.CommonResult;
import com.shang.common.ErrorCode;
import com.shang.dao.MongoChangeDao;
import com.shang.dao.ResultDao;
import com.shang.entity.Comment;
import com.shang.entity.Comment2;
import com.shang.entity.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/test")
@EnableSwagger2
public class TestController {

    @Autowired
    private MongoChangeDao mongoChangeDao;

    @Autowired
    private ResultDao resultDao;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;



    @RequestMapping(value = "/test4",method = RequestMethod.GET)
    @ApiOperation(value = "getCommentByBiSite",notes = "测试从数据库BiSite取得数据")
    public List<Comment> getCommentByBiSite(){
        String collectionName="comment";
        List<Comment> comments = mongoTemplate.find(new Query(), Comment.class, collectionName);
        return comments;
    }

    @RequestMapping(value = "/test5",method = RequestMethod.GET)
    @ApiOperation(value = "getCommentByBiSiteLimit",notes = "测试从数据库BiSite中取limit条数据")
    public List<Comment> getCommentByBiSiteLimit10(@RequestParam int limit){
        String collectionName="comment";
        Query query=new Query().limit(limit);
        List<Comment> comments = mongoTemplate.find(query, Comment.class, collectionName);
        return comments;
    }

    @RequestMapping(value = "/updateMongoDB",method = RequestMethod.POST)
    @ApiOperation(value="updateMongoDB",notes = "将表comment的数据加工到comment2中")
    public CommonResult updateMongoDB(){
        CommonResult commonResult=new CommonResult();
        for (int pageNum=1;pageNum<520;pageNum++){
        List<Comment> commentList=mongoChangeDao.getCommentListByPage(pageNum,1000);
        if (commentList==null){
            break;
        }
            for (Comment comment : commentList) {
                Comment2 comment2=new Comment2();
                comment2.setContent(comment.getContent());
                comment2.setUserName(comment.getUserName());
                comment2.setData(comment.getDate());
                comment2.setEndorse_num(comment.getEndorseNum());
                comment2.setIsMark(0);
                comment2.setScoreValue(0);
                mongoChangeDao.insertComment2(comment2);
            }
        }
        return commonResult.success();
    }

    @RequestMapping(value = "insertOneComment2",method = RequestMethod.POST)
    @ApiOperation(value = "insertOneComment2",notes = "向comment2中插入一条数据")
    public CommonResult insertOneComment2(){
        CommonResult commonResult=new CommonResult();
        String collectionName="comment";
        Query query=new Query().limit(1);
        Comment comment = mongoTemplate.findOne(query, Comment.class, collectionName);
        Comment2 comment2=new Comment2();
        comment2.setComment_id(0);
        comment2.setUserName(comment.getUserName());
        comment2.setContent(comment.getContent());
        comment2.setData(comment.getDate());
        comment2.setScoreValue(0);
        comment2.setEndorse_num(comment.getEndorseNum());
        comment2.setIsMark(0);
        mongoTemplate.insert(comment2,"comment2");
        return commonResult.success();
    }


    @RequestMapping(value = "addResult",method = RequestMethod.POST)
    @ApiOperation(value = "addResult",notes = "增加一条result记录")
    public CommonResult addResult(@RequestParam String date){
        int isSuccess = resultDao.addResult(date);
        if (isSuccess==1){
            return CommonResult.success();
        }
        else {
            return CommonResult.fail(ErrorCode.SYSTEM_ERROR.getCode(), ErrorCode.SESSION_TIME_OUT.getMsg());
        }
    }

    @RequestMapping(value = "/addResultTest",method = RequestMethod.POST)
    @ApiOperation(value = "addResultTest",notes = "增加一条测试result记录")
    public CommonResult addResultTest(@RequestBody Result result){
        Result result1 = mongoTemplate.insert(result, "result");
        if (result1!=null){
            return CommonResult.success();
        }
        else {
            return CommonResult.fail(ErrorCode.SYSTEM_ERROR.getCode(), ErrorCode.SESSION_TIME_OUT.getMsg());
        }
    }

}
